package com.chzc.fm.duotin.ui.channel;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.adapter.listview.TrackListAdapter;
import com.chzc.fm.duotin.bean.channel.Play;
import com.chzc.fm.duotin.ui.channel.event.PlayEvent;
import com.chzc.fm.duotin.utils.DuotinTask;
import com.chzc.fm.duotin.utils.FastBlur;
import com.chzc.fm.duotin.utils.FileUtils;
import com.chzc.fm.duotin.utils.HttpUtil;
import com.chzc.fm.duotin.utils.LogUtil;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/6/30.
 */
public class PlayTrackListActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageView imageView;
    private TextView tvmenu,tvclose;
    private ListView listView;
    private LinearLayout linearLayout;

    private int position = 0;

    private List<Play> playlist = new ArrayList<>();

    private List<Map<String, Object>> listItems;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playtractlist);

        initViews();

        initDatas();
    }

    public void initViews()
    {
        imageView = (ImageView) findViewById(R.id.playtractlist_blur_iv);
        tvmenu = (TextView) findViewById(R.id.playtractlist_menu_tv);
        tvclose = (TextView) findViewById(R.id.playtractlist_close_tv);
        listView = (ListView) findViewById(R.id.playtractlist_conten_lv);
        linearLayout = (LinearLayout) findViewById(R.id.playlist_ll);

        linearLayout.setOnClickListener(this);
        tvclose.setOnClickListener(this);
    }

//    @Override
//    protected void onResume() {
//        super.onResume();
//        Animation animation = AnimationUtils.loadAnimation(PlayTrackListActivity.this,R.anim.activity_open);
//        linearLayout.startAnimation(animation);
//    }

    public void initDatas()
    {
        playlist = (List<Play>) getIntent().getSerializableExtra("playlist");
        LogUtil.w("playlist.size >>>>>" + playlist.size());
        showBlurImag(position);

        listItems = getListItems();
        TrackListAdapter adapter = new TrackListAdapter(PlayTrackListActivity.this,listItems);
        listView.setAdapter(adapter);

    }

    private List<Map<String, Object>> getListItems() {
        List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
        for(int i = 0; i <playlist.size(); i++)
        {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("title", playlist.get(i).getTitle());               //标题
            map.put("content", playlist.get(i).getDescribe());     //内容描述
            listItems.add(map);
        }
        return listItems;
    }


    /**
     * 显示模糊图
     * @param position
     */
    public void showBlurImag(int position)
    {
        Play play = playlist.get(position);
        final String imagurl = play.getImage_url();

        DuotinTask.IDownloadListener downloadListener = new DuotinTask.IDownloadListener() {
            @Override
            public void upgradeProgress(float progress) {

            }

            @Override
            public void onComplete(File file) {

                final Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        FastBlur.blur(PlayTrackListActivity.this,bitmap,imageView);
                    }
                });


            }

            @Override
            public void onFail(String msg) {

            }

            @Override
            public void start() {

            }
        };

        new DuotinTask(new DuotinTask.IDownloadRequest() {
            @Override
            public Object doRequest(DuotinTask.IDownloadListener listener) {
                return HttpUtil.download(imagurl, FileUtils.DIR_IMAGE,FileUtils.getFileNameByHashCode(imagurl),listener);
            }
        },downloadListener).execute();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.playlist_ll:
                finish();
                overridePendingTransition(0,R.anim.activity_close);
                break;
            case R.id.playtractlist_close_tv:
                finish();
                overridePendingTransition(0,R.anim.activity_close);
                break;
        }
    }
    @Override
    public void onBackPressed() {
        EventBus.getDefault().post(new PlayEvent("传递消息"));
        super.onBackPressed();

    }
}
